-- https://github.com/folke/which-key.nvim

local pack = {}

pack.lazy = {
    "folke/which-key.nvim",
    event = { "VeryLazy" },
    dependencies = {
        -- 移除nvim-web-devicons依赖，避免加载图标插件
        --"echasnovski/mini.icons",移除echasnovski/mini.icons依赖，避免加载图标
    },
}

function pack.before_load()
    -- 不在这里 require which-key，因为插件还没有安装
end

function pack.load()
    -- 在这里 require which-key，因为此时插件已经由 lazy.nvim 安装
    pack.which_key = require("which-key")
    
    -- 设置自定义高亮组，使用深灰色背景
    vim.api.nvim_set_hl(0, "WhichKeyNormal", { bg = "#2d2d2d", fg = "#ffffff" })
    vim.api.nvim_set_hl(0, "WhichKeyBorder", { bg = "#2d2d2d", fg = "#5f5f5f" })
    
    pack.which_key.setup({
        plugins = {
            spelling = {
                enabled = true,
                suggestions = 20,
            },
            -- 禁用可能导致警告的插件功能
            marks = false,
            registers = false,
            presets = {
                operators = false,
                motions = false,
                text_objects = false,
                windows = false,
                nav = false,
                z = false,
                g = false,
            },
        },
        icons = {
            breadcrumb = "",
            separator = "  ",
            group = "",  -- 移除组图标以避免字符位置问题
        },
        win = {
            -- 设置背景色为深灰色，避免纯黑色
            wo = {
                winhighlight = "NormalFloat:WhichKeyNormal,FloatBorder:WhichKeyBorder",
            },
        },
        spec = {
            { "<leader>b", group = "Buffers" },
            { "<leader>c", group = "Code" },
            { "<leader>d", group = "Debug" },
            { "<leader>f", group = "Find" },
            { "<leader>g", group = "Git" },
            { "<leader>l", group = "Lazy" },
            { "<leader>h", group = "Http" },
            { "<leader>r", group = "Replace" },
            { "<leader>u", group = "Upload" },
            { "<leader>s", group = "Session" },
            { "<leader>t", group = "Terminal" },
            { 
                "<leader>rw",
                desc = "Replace Word To ..."
            },
            -- Comment.nvim 已经配置了 gc 前缀的多级映射
            -- 保留这些描述以显示在 which-key 中
            { 
                "gc", 
                desc = "Toggle comment",
            },
            { 
                "gcc", 
                desc = "Toggle comment line",
            },
            { 
                "gcb", 
                desc = "Toggle comment block",
            },
            { 
                "gck", 
                desc = "Comment insert above",
            },
            { 
                "gcj", 
                desc = "Comment insert below",
            },
            { 
                "gca", 
                desc = "Comment insert end of line",
            },
        },
    })
end

function pack.after_load()
    -- 在插件加载后进行配置 - 使用新版本的 which-key 规范
    -- 注意：组定义（如Terminal、Session）已经在setup的spec中定义
    pack.which_key.add({
        { "<leader>tf", desc = "Float terminal" },
        { "<leader>tn", desc = "New Terminal" },
        { "<leader>sl", desc = "Resume the session" },
        -- 移除 <leader>tr 的配置，因为我们已经移除了这个功能
        -- { "<leader>tr", desc = "New Terminal (spiral)" },
        -- 移除 lazygit 相关的配置
        -- { "<leader>tg", desc = "Open lazygit" },
        -- 移除无意义的全部终端选项
        -- { "<leader>ta", desc = "All terminals" },
    })
end

return pack